﻿<%@ WebHandler Language="C#" Class="customermodifyupload" %>

using System;
using System.Web;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;




public class customermodifyupload : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        context.Response.Headers.Add("connection", "keep-alive");
        context.Response.CacheControl = "no-cache";
        context.Response.AddHeader("Connection", "Keep-Alive");
        context.Response.Headers.Set("Connection", "Keep-Alive");
        context.Response.Headers.Add("Connection", "Keep-Alive");        
        context.Response.AppendHeader("Connection", "Keep-Alive");
        string detailid = HttpContext.Current.Request.Params["id_batch"];
        string type_id = HttpContext.Current.Request.Params["fb"];
        if (detailid.IndexOf(",") > 0)
        {
            var array = detailid.Split(',');
            for (int i = 0; i < array.Length; i++)
            {
                SaveFile(array[i].ToString(), type_id);
            }
        }
        else
        {
            SaveFile(detailid, type_id);
        }
    }

    
    MsSqlHelper mssql = new MsSqlHelper();
    Common Init = new Common();

    /// <summary>
    /// 文件保存操作
    /// </summary>
    /// <param name="basePath"></param>
    private void SaveFile(string detailid, string type_id="", string basePath = "~/Upload/Images/")
    {
        var name = string.Empty;
        string typename = "";
        switch (type_id)
        {
            case "yyzz":
	    	    typename="营业执照";
                break;
            case "ypzlgf":
	    	    typename="经营/生产许可证";
                break;
            case "gspzs":
	    	    typename="GSP/GMP证书";
                break;
            case "zlbzxy":
	    	    typename="质量保证协议";
                break;
            case "yzyp":
	    	    typename="样张样票";
                break;
            case "fpyz":
	    	    typename="发票样张";
                break;
            case "yinzhang":                
	    	    typename="单位印章";
                break;
            case "frwts":
	    	    typename="法人委托书";
                break;
            case "bwtrsfz":
	    	    typename="被委托人身份证";
                break;
            case "khxkz":
	    	    typename="开户许可证";
                break;
	        case "zlbznldcb":
	    	    typename="质量保证能力调查表";
                break;
	        case "dwbgjl":
	    	    typename="单位变更记录";
                break;
            case "kpxx":
	    	    typename="开票信息";
                break;
            case "dbthxkpj":
	    	    typename="蛋白同化制剂、肽类激素许可批件";
                break;
            case "yljgzyxkz":
	    	    typename="医疗机构执业许可证";
                break;
            case "ylqxjyxkz":
	    	    typename="医疗器械经营许可证";
                break;
            case "spltxkz":
	    	    typename="食品经营许可证";
                break;
	        case "qyndgsb":
	    	    typename="企业年度公示表";
                break;
	        case "swdlz":
	    	    typename="税务登记证";
                break;
	        case "hetong":
	    	    typename="合同";
                break;
            default:
                typename = "";
                break;
        }     
        string fullbasePath = (basePath.IndexOf("~") > -1) ? HttpContext.Current.Server.MapPath(basePath) :
        fullbasePath = basePath;
        HttpFileCollection files = HttpContext.Current.Request.Files;
        HttpPostedFile file;
        string folderdt = DateTime.Now.ToString("yyyy'/'MM'/'dd");
        if (!Directory.Exists(fullbasePath))
            Directory.CreateDirectory(fullbasePath);
        if (!Directory.Exists(basePath + folderdt + "\\"))
            Directory.CreateDirectory(fullbasePath + folderdt + "\\");
        
        string f = "00000000";
        for (int i = 0; i < HttpContext.Current.Request.Files.Count; ++i)
        {
            file = HttpContext.Current.Request.Files[i];
            f= Init.maxbh().ToString() ;
            string full = fullbasePath + folderdt + "\\" + f + Path.GetExtension(file.FileName);
            string sql = "insert into [ImageDoc](imageid,type_id,leibie,detailid,extend,beactive,url,fzxx5,lastmodifytime)values('" + f + "',rtrim('" + type_id + "'),'" + typename + "',rtrim('" + detailid + "'),'" + Path.GetExtension(file.FileName) + "','是','" + basePath.Replace("~", "") + folderdt + "/" + f + Path.GetExtension(file.FileName) + "','" + file.FileName + "',CONVERT(varchar(100), GETDATE(), 120))";
            int success = mssql.ExecuteSql(sql);
                
            files[0].SaveAs(full);
            var _result = "{\"code\":0,\"msg\":\"\u4e0a\u4f20\u6210\u529f" + f + "|" + type_id + "|" + typename + "|" + file.FileName + "\"}";
            HttpContext.Current.Response.Write(_result);
        }
    }
     
    public bool IsReusable {
        get {
            return false;
        }
    }

}